package com.etc;

import jdk.nashorn.internal.parser.Token;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * @Auther: zhezhe
 * @Date: 2018/10/2517:58
 * @Description:
 */
public class AvgSourceMap extends Mapper<LongWritable, Text,Text, IntWritable>{
    @Override
    protected void map(LongWritable key, Text value, Context context) throws
            IOException, InterruptedException {
        String line = value.toString();
        //按行分割数据.StringTokenize类将单词从字符串中分离出来的功能,各个单词依据分隔符被分成一个个token
        StringTokenizer tok = new StringTokenizer(line,"\n");
        //判断是否已到结尾
        while (tok.hasMoreElements()){
            //按空格分割每行数据,nextToken()方法,返回下一个Token
            StringTokenizer tok1 = new StringTokenizer(tok.nextToken());
            String name = tok1.nextToken();
            String source = tok1.nextToken();
            context.write(new Text(name),new IntWritable(Integer.parseInt(source)));
        }
    }
}
